﻿using System;
using System.IO;

using Bpms.Common;

namespace Bpms.Config
{
    /// <summary>
    /// email配置文件操作类
    /// </summary>
    public class EmailConfigFileManager : BaseConfigFileManager
    {
        private static EmailConfigInfo _configinfo;

        /// <summary>
        /// 文件修改时间
        /// </summary>
        private static DateTime _fileoldchange;

        /// <summary>
        /// 初始化文件修改时间和对象实例
        /// </summary>
        static EmailConfigFileManager()
        {
            _fileoldchange = System.IO.File.GetLastWriteTime(ConfigFilePath);
            _configinfo = (EmailConfigInfo)BaseConfigFileManager.DeserializeInfo(ConfigFilePath, typeof(EmailConfigInfo));
            //_configinfo = new EmailConfigInfo();
        }

        /// <summary>
        /// 当前的配置实例
        /// </summary>
        public new static IConfigInfo ConfigInfo
        {
            get { return _configinfo; }
            set { _configinfo = (EmailConfigInfo)value; }
        }


        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        public static string filename = null;


        /// <summary>
        /// 获取配置文件所在路径
        /// </summary>
        public new static string ConfigFilePath
        {
            get
            {
                if (filename == null)
                {
                    filename = Utility.GetMapPath("~/config/email.config");
                    if (!File.Exists(filename))
                    {
                        //File.Create(filename);
                        ConfigInfo = new EmailConfigInfo();
                        new EmailConfigFileManager().SaveConfig();
                    }
                }

                return filename;
            }
        }

        /// <summary>
        /// 返回配置类实例
        /// </summary>
        /// <returns></returns>
        public static EmailConfigInfo LoadConfig()
        {
            ConfigInfo = BaseConfigFileManager.LoadConfig(ref _fileoldchange, ConfigFilePath, ConfigInfo);
            return ConfigInfo as EmailConfigInfo;
        }

        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <returns></returns>
        public override bool SaveConfig()
        {
            return base.SaveConfig(ConfigFilePath, ConfigInfo);
        }
    }
}
